package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jl.domain.ReportJlCjlsfgsfjlbBaDetail;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlsfgsfjlbBaDetailVo;
import com.ruoyi.report_jl.mapper.ReportJlCjlsfgsfjlbBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjlsfgsfjlbBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlsfgsfjlbBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlCjlsfgsfjlbBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlCjlsfgsfjlbBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlCjlsfgsfjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料筛分试验检测记录（干筛法）Service业务层处理
 *
 * @author tll
 * @date 2024-05-22
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjlsfgsfjlbBsInfoServiceImpl implements IReportJlCjlsfgsfjlbBsInfoService {

    private final ReportJlCjlsfgsfjlbBsInfoMapper baseMapper;
    private final ReportJlCjlsfgsfjlbBaDetailMapper reportJlCjlsfgsfjlbBaDetailMapper;

    /**
     * 查询粗集料筛分试验检测记录（干筛法）
     */
    @Override
    @FIllBasicInfo
    public ReportJlCjlsfgsfjlbBsInfoVo queryById(Long excelId){
        // 主表
        ReportJlCjlsfgsfjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportJlCjlsfgsfjlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportJlCjlsfgsfjlbBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportJlCjlsfgsfjlbBaDetail::getCreateTime);
        List<ReportJlCjlsfgsfjlbBaDetailVo> detailList = reportJlCjlsfgsfjlbBaDetailMapper.selectVoList(lqw);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询粗集料筛分试验检测记录（干筛法）列表
     */
    @Override
    public TableDataInfo<ReportJlCjlsfgsfjlbBsInfoVo> queryPageList(ReportJlCjlsfgsfjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjlsfgsfjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjlsfgsfjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料筛分试验检测记录（干筛法）列表
     */
    @Override
    public List<ReportJlCjlsfgsfjlbBsInfoVo> queryList(ReportJlCjlsfgsfjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlCjlsfgsfjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjlsfgsfjlbBsInfo> buildQueryWrapper(ReportJlCjlsfgsfjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjlsfgsfjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlCjlsfgsfjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlCjlsfgsfjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlCjlsfgsfjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlCjlsfgsfjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlCjlsfgsfjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlCjlsfgsfjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlCjlsfgsfjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlCjlsfgsfjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlCjlsfgsfjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlCjlsfgsfjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlCjlsfgsfjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlCjlsfgsfjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlCjlsfgsfjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlCjlsfgsfjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlCjlsfgsfjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlCjlsfgsfjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlCjlsfgsfjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlCjlsfgsfjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlCjlsfgsfjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlCjlsfgsfjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlCjlsfgsfjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlCjlsfgsfjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlCjlsfgsfjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlCjlsfgsfjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getShaifhzzlItem1() != null, ReportJlCjlsfgsfjlbBsInfo::getShaifhzzlItem1, bo.getShaifhzzlItem1());
        lqw.eq(bo.getSunhItem1() != null, ReportJlCjlsfgsfjlbBsInfo::getSunhItem1, bo.getSunhItem1());
        lqw.eq(bo.getSunhlItem1() != null, ReportJlCjlsfgsfjlbBsInfo::getSunhlItem1, bo.getSunhlItem1());
        lqw.eq(bo.getShaifhzzlItem2() != null, ReportJlCjlsfgsfjlbBsInfo::getShaifhzzlItem2, bo.getShaifhzzlItem2());
        lqw.eq(bo.getSunhItem2() != null, ReportJlCjlsfgsfjlbBsInfo::getSunhItem2, bo.getSunhItem2());
        lqw.eq(bo.getSunhlItem2() != null, ReportJlCjlsfgsfjlbBsInfo::getSunhlItem2, bo.getSunhlItem2());
        return lqw;
    }

    /**
     * 新增粗集料筛分试验检测记录（干筛法）
     */
    @Override
    public Boolean insertByBo(ReportJlCjlsfgsfjlbBsInfoBo bo) {
        ReportJlCjlsfgsfjlbBsInfo add = BeanUtil.toBean(bo, ReportJlCjlsfgsfjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改粗集料筛分试验检测记录（干筛法）
     */
    @Override
    public Boolean updateByBo(ReportJlCjlsfgsfjlbBsInfoBo bo) {
        // 主表
        ReportJlCjlsfgsfjlbBsInfo update = BeanUtil.toBean(bo, ReportJlCjlsfgsfjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean upd = baseMapper.updateById(update) > 0;
        // 子表
        boolean upd2 = false;
        boolean insert = false;
        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
            // 根据excelId查询明细
            QueryWrapper<ReportJlCjlsfgsfjlbBaDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("excel_id", bo.getExcelId());
            List<ReportJlCjlsfgsfjlbBaDetail> list = reportJlCjlsfgsfjlbBaDetailMapper.selectList(queryWrapper);
            for (ReportJlCjlsfgsfjlbBaDetail det : bo.getDetailList()) {
                if (ObjectUtils.isEmpty(list)) {
                    // 新增
                    insert = reportJlCjlsfgsfjlbBaDetailMapper.insert(det) > 0;
                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
                    // 修改
                    upd2 = reportJlCjlsfgsfjlbBaDetailMapper.updateById(det) > 0;
                } else {
                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
                }

            }
        }
        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjlsfgsfjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料筛分试验检测记录（干筛法）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
